// 获得评论列表数据
$.ajax({
    type: 'get',
    url: '/comments',
    success: function (response) {
        console.log(response);
        let html = template('commentsTpl', response);
        $('#commentsBox').html(html);
        let pageHtml = template('pageTpl', response);
        $('#pageBox').html(pageHtml);
    }
});

// 实现分页功能
function changePage(page) {
    $.ajax({
        type: 'get',
        url: '/comments',
        data: {
            page: page
        },
        success: function (response) {
            let html = template('commentsTpl', response);
            $('#commentsBox').html(html);
            let pageHtml = template('pageTpl', response);
            $('#pageBox').html(pageHtml);
        }
    })
}

// 评论审核
$('#commentsBox').on('click', '.status', function () {
    let id = $(this).attr('data-id');
    let status = $(this).attr('data-status');
    console.log(status);
    $.ajax({
        type: 'put',
        url: '/comments/' + id,
        data: {
            state: status == 0 ? 1 : 0,
        },
        success: function (response) {
            location.reload();
        }
    })
});

// 删除评论
$('#commentsBox').on('click', '.delete', function () {
    if (confirm('您真的要删除这条评论吗？')) {
        let id = $(this).attr('data-id');
        $.ajax({
            type: 'delete',
            url: '/comments/' + id,
            success: function () {
                location.reload();
            }
        })
    }
});